package com.woyao.core.util;

import android.app.Activity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.Spinner;

import com.woyao.R;
import com.woyao.core.BusinessCategoryLoader;
import com.woyao.core.model.BusinessCategory;
import com.woyao.core.model.GetBusinessCategoryResponse;

import java.util.ArrayList;

/**
 * Created by summerwind on 2016-06-02.
 */
public class BusinessSelect {
    private Activity context;
    private LinearLayout parent;
    private Spinner[] categoryList;
    private ArrayList<BusinessCategory>[] categoryData;
    private BusinessCategory none;

    public BusinessSelect(){
        categoryList = new Spinner[3];

        categoryData = new ArrayList[3];

        none = new BusinessCategory();
        none.setNo("0");
        none.setName("请选择");
    }

    public void construct() {

        categoryList[0] = new Spinner(context);
        categoryList[1] = new Spinner(context);
        categoryList[2] = new Spinner(context);

        categoryList[0].setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                if(position>0){
                    BusinessCategoryLoader loader = new BusinessCategoryLoader(context, categoryData[0].get(position).getNo());
                    loader.setOnSuccessListener(new BusinessCategoryLoader.OnSuccessListener() {
                        @Override
                        public void onSuccess(GetBusinessCategoryResponse response) {
                            if(response != null && response.getCategoryList() != null){
                                categoryData[1] = response.getCategoryList();
                                render(2);
                            }else{
                                categoryData[1] = new ArrayList<BusinessCategory>();
                                render(2);

                            }
                        }
                    });
                    loader.load();
                }else{
                    categoryData[1] = new ArrayList<BusinessCategory>();
                    render(2);
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        categoryList[1].setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                if(position>0){
                    BusinessCategoryLoader loader = new BusinessCategoryLoader(context, categoryData[1].get(position).getNo());
                    loader.setOnSuccessListener(new BusinessCategoryLoader.OnSuccessListener() {
                        @Override
                        public void onSuccess(GetBusinessCategoryResponse response) {
                            if(response != null&& response.getCategoryList()!=null){
                                categoryData[2] = BusinessCategoryLoader.processLevel(response.getCategoryList());
                                render(3);
                            }else {
                                categoryData[2] = new ArrayList<BusinessCategory>();
                                render(3);
                            }
                        }
                    });
                    loader.load();
                }else{
                    categoryData[2] = new ArrayList<BusinessCategory>();
                    render(3);
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        parent.addView(categoryList[0]);
        parent.addView(categoryList[1]);
        parent.addView(categoryList[2]);

        BusinessCategoryLoader loader = new BusinessCategoryLoader(context, "0");
        loader.setOnSuccessListener(new BusinessCategoryLoader.OnSuccessListener() {
            @Override
            public void onSuccess(GetBusinessCategoryResponse response) {
                if(response!=null&&response.getCategoryList()!=null){
                    categoryData[0] = response.getCategoryList();
                    render(1);
                }else{
                    categoryData[0] = new ArrayList<BusinessCategory>();
                    render(1);
                }
            }
        });
        loader.load();
    }

    public String getNo(int level){
        int pos = categoryList[level-1].getSelectedItemPosition();
        if(categoryData[level-1].size()>pos){
            return categoryData[level-1].get(pos).getNo();
        }{
            return "0";
        }
    }
    private void render(int level){
        categoryData[level-1].add(0, none);
        String[] list = new String[categoryData[level-1].size()];
        for(int index = 0;index<list.length;index++){
            list[index] = categoryData[level-1].get(index).getName();

        }
        categoryList[level-1].setAdapter(new ArrayAdapter<String>(
                context,
                R.layout.support_simple_spinner_dropdown_item,
                list
        ));
        categoryList[level-1].setSelection(0);
    }
    public Activity getContext() {
        return context;
    }

    public void setContext(Activity context) {
        this.context = context;
    }

    public LinearLayout getParent() {
        return parent;
    }

    public void setParent(LinearLayout parent) {
        this.parent = parent;
    }
}
